<template>
  <!-- 采购请购单 -->
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="请购单号">
              <a-input placeholder="请输入请购单号" v-model="queryParam.serial"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="供应商">
              <j-multi-select-tag
                placeholder="请选择供应商"
                v-model="queryParam.supplierId"
                dict-code="hf_supplier,name,id"
              />
            </a-form-item>
          </a-col>
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="供应商订单号">
              <a-input v-model="queryParam.supplierOrderSerial" />
            </a-form-item>
          </a-col>
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="关联销售订单号">
              <a-input type="list" v-model="queryParam.sourceSerial" />
            </a-form-item>
          </a-col>
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="单据日期">
              <j-date
                type="list"
                v-model="queryParam._documentDate"
                @change="handleDateInterval"
                style="width: 100%"
                :disabled="queryParam._dateInterval"
              />
            </a-form-item>
          </a-col>
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="日期区间">
              <a-range-picker
                v-model="queryParam._dateInterval"
                @change="handleDateInterval"
                :disabled="queryParam._documentDate"
              />
            </a-form-item>
          </a-col>
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="物料代码">
              <a-input type="list" v-model="queryParam.materialCode" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :xs="colSpan.xs" :sm="colSpan.sm" :md="colSpan.md" :lg="colSpan.lg" :xl="colSpan.xl">
            <a-form-item label="物料名称">
              <a-input type="list" v-model="queryParam.materialName" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls('采购请购单')">导出</a-button>
      <a-upload
        :action="importExcelUrl"
        :headers="tokenHeader"
        :multiple="false"
        :showUploadList="false"
        name="file"
        @change="handleImportExcel"
      >
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>
      <a-button type="primary" icon="calculator" @click="handleMPR" style="background-color:green;border-color: green"
        >MRP运算</a-button
      >
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :customRow="clickThenSelect"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :scroll="{ x: true }"
        bordered
        class="j-table-force-nowrap"
        rowKey="id"
        size="middle"
        @change="handleTableChange"
      >
        <template slot="isClosed" slot-scope="text">
          <a-tag :color="text === '未关闭' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.status === 0">编辑</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleDelete(record.id)" v-if="record.status === 0">删除</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a-divider type="vertical" v-if="record.status === 1" />
          <a @click="handleUndoCheck(record)" v-if="record.status === 1">反审核</a>
          <a-divider type="vertical" v-if="record.status === 1" />
          <a @click="handleCheck(record)" v-if="record.status === 0">审核</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleClose(record)" v-if="record.isClosed === 0">关闭</a>
          <a @click="handleUndoClose(record)" v-if="record.isClosed === 1">反关闭</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane key="1" tab="采购请购单详情">
        <HfPurchaseRequestDetailList
          :mainId="selectedMainId"
          :sourceSerial="sourceSerial"
          :supplierId="selectedSupplierId"
        />
      </a-tab-pane>
    </a-tabs>

    <hfPurchaseRequest-modal ref="modalForm" @ok="modalFormOk"></hfPurchaseRequest-modal>
    <MPRCalcModal ref="mrpModal"></MPRCalcModal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfPurchaseRequestModal from './modules/HfPurchaseRequestModal'
import { getAction, putAction } from '@/api/manage'
import HfPurchaseRequestDetailList from './HfPurchaseRequestDetailList'
import '@/assets/less/TableExpand.less'
import { Modal } from 'ant-design-vue'
import MPRCalcModal from '@views/purchase/MPRCalc/MPRCalcModal.vue'

export default {
  name: 'HfPurchaseRequestList',
  mixins: [JeecgListMixin],
  components: {
    MPRCalcModal,
    HfPurchaseRequestDetailList,
    HfPurchaseRequestModal
  },
  data() {
    return {
      description: '采购请购单管理页面',
      // 表头
      columns: [
        {
          title: '请购单号',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'documentType_dictText'
        },
        {
          title: '供应商',
          align: 'center',
          dataIndex: 'supplierId_dictText'
        },
        {
          title: '供应商订单号',
          align: 'center',
          dataIndex: 'supplierOrderSerial'
        },
        {
          title: '预计到货日期',
          align: 'center',
          dataIndex: 'arrivalTime',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '实际到货日期',
          align: 'center',
          dataIndex: 'actualArrivalTime',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '单据日期',
          align: 'center',
          sorter: true,
          dataIndex: 'documentTime',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '请购人',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '审核标志',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '关闭状态',
          align: 'center',
          dataIndex: 'isClosed_dictText',
          scopedSlots: { customRender: 'isClosed' }
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/purchase/hfPurchaseRequest/list',
        delete: '/purchase/hfPurchaseRequest/delete',
        deleteBatch: '/purchase/hfPurchaseRequest/deleteBatch',
        exportXlsUrl: '/purchase/hfPurchaseRequest/exportXlsTest',
        importExcelUrl: 'purchase/hfPurchaseRequest/importExcelTest',
        check: '/purchase/hfPurchaseRequest/check',
        undoCheck: '/purchase/hfPurchaseRequest/undoCheck',
        close: '/purchase/hfPurchaseRequest/close',
        undoClose: '/purchase/hfPurchaseRequest/undoClose'
      },
      dictOptions: {},
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      sourceSerial: '',
      selectedSupplierId: '',
      purchaseOrderSerial: '',
      superFieldList: []
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleUndoCheck(record) {
      Modal.confirm({
        title: '确认反审核?',
        content: '反审核后将会删除关联生成的采购订单',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          putAction(this.url.undoCheck, record).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
    handleClose(record) {
      Modal.confirm({
        title: '确认关闭?',
        content: '是否确认关闭该采购请购单',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          putAction(this.url.close, record).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
    handleUndoClose(record) {
      putAction(this.url.undoClose, record).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    initDictConfig() {},
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
      this.selectedSupplierId = ''
      this.sourceSerial = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedSupplierId = selectionRows[0].supplierId
      this.sourceSerial = selectionRows[0].serial
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      params['status'] = 0
      params['isClosed'] = 0
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleDateInterval(value) {
      if (typeof value === 'string') {
        this.queryParam['documentDate_begin'] = value
        this.queryParam['documentDate_end'] = value
        return
      }
      this.queryParam['documentDate_begin'] = value[0].format('YYYY-MM-DD')
      this.queryParam['documentDate_end'] = value[1].format('YYYY-MM-DD')
    },
    handleMPR() {
      this.$refs.mrpModal.visible = true
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
